#include "MainComponent.h"
#include <array>
#include "JuceHeader.h"

//==============================================================================
// 内容层模板，所有 node 都加到 contentLayer 上，只缩放 contentLayer，不缩放背景
MainComponent::MainComponent()
{
    contentLayer = std::make_unique<juce::Component>();
    addAndMakeVisible(*contentLayer);
    contentLayer->setInterceptsMouseClicks(false, true);

    node1 = std::make_unique<NodeWithButton>("Button Node");
    node1->setNodePos({200, 100});
    node1->setSize(200, 100);
    contentLayer->addAndMakeVisible(*node1);

    node2 = std::make_unique<NodeWithLabel>("Text Node", "Hello Node!");
    node2->setNodePos({450, 200});
    node2->setSize(200, 100);
    contentLayer->addAndMakeVisible(*node2);


    setSize((int)default_width, (int)default_height);

    contentLayer->setBounds(getLocalBounds());

    // 初始化transform，保证初始可拖动
    juce::AffineTransform t =
        juce::AffineTransform::translation(-panOffset.x, -panOffset.y)
        .scaled(scale, scale)
        .translated(panOffset.x, panOffset.y);
    contentLayer->setTransform(t);

}

void MainComponent::paint(juce::Graphics& g)
{
    // 只画背景，不缩放
    g.fillAll(juce::Colours::white.fromRGB(30, 30, 30));

}

void MainComponent::resized()
{
    contentLayer->setBounds(getLocalBounds());
    updateContentTransform();
}

void MainComponent::getNextAudioBlock(const juce::AudioSourceChannelInfo& bufferToFill)
{
    bufferToFill.clearActiveBufferRegion();
    auto originalPhase = phase;

    for (auto chan = 0; chan < bufferToFill.buffer->getNumChannels(); ++chan)
    {
        phase = originalPhase;

        auto* channelData = bufferToFill.buffer->getWritePointer (chan, bufferToFill.startSample);

        for (auto i = 0; i < bufferToFill.numSamples ; ++i)
        {
            channelData[i] = amplitude * std::sin (phase);

            // increment the phase step for the next sample
            phase = std::fmod (phase + phaseDelta, MathConstants<float>::twoPi);
        }
    }
}

// 设置transform的统一方法
void MainComponent::updateContentTransform()
{
    juce::AffineTransform t =
        juce::AffineTransform::translation(panOffset.x, panOffset.y)
        .scaled(scale, scale);
    contentLayer->setTransform(t);
}

void MainComponent::mouseDown(const juce::MouseEvent& e)
{
    (void)e;
}

void MainComponent::mouseDrag(const juce::MouseEvent& e)
{
    (void)e;
}

void MainComponent::mouseMove(const juce::MouseEvent& e)
{
    (void)e;
}

void MainComponent::mouseUp(const juce::MouseEvent& e)
{
    (void)e;
}

void MainComponent::mouseWheelMove(const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
{
    float oldScale = scale;
    scale = juce::jlimit(0.3f, 3.0f, scale + wheel.deltaY * 0.1f);

    if (scale != oldScale)
    {
        auto mousePos = e.getPosition().toFloat();

        // 计算鼠标在内容层的逻辑坐标
        juce::Point<float> logicalPos = (mousePos - panOffset) / oldScale;

        // 缩放后，panOffset 应该让 logicalPos * scale + panOffset == mousePos
        panOffset = mousePos - logicalPos * scale;

        updateContentTransform();
    }
}
